angular.module('Controller')
.controller('QgyLayoutPictureEditCtrl',($scope,urls,$localStorage,NgTableParams,commonUtil,cryptUtil,wfAlert,commonConstant,$http, $uibModalInstance, items)->
    type=items.type
    $scope.type=items.type
    $scope.data=items.data
    $scope.info=items.info
    $scope.modalTitle=if type=='new' then '新增图片' else '编辑图片'
    $scope.modalEnableList=commonConstant.enabledList()
    _.defer(()->
        $('#photo-upload-input').fileinput({
            language: 'zh',
            maxFileCount: 1,
            allowedFileExtensions : ['jpg', 'png','gif'],
            uploadUrl: urls.backend + urls.imageUploadTo +  "/image?token=" + $localStorage['token'],
            uploadAsync: true,
            enctype: 'multipart/form-data',
            showCaption: false
        }).on('fileuploaded', (event, data, id, index)->
            if data.response?.code==0
                console.log(data.response?.message)
                $scope.result = data.response?.message
                saveUrl = data.response.message.saveUrl+""
                if(saveUrl.indexOf("http")==0)
                    $scope.data.url = saveUrl
                else
                    $scope.data.url=urls.backend+'/public/'+$scope.result['saveUrl']

                $scope.showPhotoUpload=false
                $scope.$digest()
            else
                wfAlert.toastError(data.response.message)
        )
    )
    loadApartment=()->
        requestData={
            page: 0,
            size: 999
        }
        $http.get('/apartment',params:requestData)
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=resp.message.content
                if $scope.apartmentList?.length>0
                    $scope.data.apartmentId=$scope.apartmentList[0].id
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    _.extend $scope,
        newData:()->
            reqData={
#                "apartmentId":0
                "layoutId":$scope.data.layoutId,  #房型id
                "url":$scope.data.url,  #图片地址
                "type":1, #图片类型$scope.data.type,暂时用不到多类型
                "sorting":parseInt($scope.data.sorting), #排序
                "imageContent":$scope.data.imageContent, #图片内容
                "description" : $scope.data.description
                "enabled" : $scope.data.enabled
            }
            $http.post('/layoutPicture',reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
        editData:()->
            reqData={
#                "layoutId":$scope.data.layoutId,  #房型id
                "url":$scope.data.url,  #图片地址
#                "type":$scope.data.type, #图片类型
                "sorting":parseInt($scope.data.sorting), #排序
                "imageContent":"imageContent", #图片内容
#                "apartmentId" : $scope.data.apartmentId
                "imageContent" : $scope.data.imageContent
                "description" :$scope.data.description
                "enabled" : $scope.data.enabled
            }
            $http.put("/layoutPicture/#{$scope.data.id}",reqData)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo($scope.modalTitle+'成功')
                    $uibModalInstance.close('done')
                else
                    wfAlert.toastError(resp.message)
            )
    $scope.confirm = ()->
        if type=='new'
            $scope.newData()
        else
            $scope.editData()
    $scope.cancel =()->
        $uibModalInstance.dismiss('cancel')
)